package cn.com.infcn;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBElement;

import org.glassfish.jersey.client.ClientResponse;
import org.junit.Test;

public class UserClient {
	// @Test
	// public void insertUser(){
	// r = Client.create().resource("http://localhost:8081/jersey/api/users");
	// Form form = new Form();
	// form.add("userId", "002");
	// form.add("userName", "ZhaoHongXuan");
	// form.add("userAge", 23);
	// ClientResponse response = r.type(MediaType.APPLICATION_FORM_URLENCODED)
	// .post(ClientResponse.class, form);
	// System.out.println(response.getStatus());
	// }
	@Test
	public void findUser() {
		Client c = ClientBuilder.newClient();
		WebTarget target = c.target("http://localhost:8080/myapp/users/001");
		
		User user = target.request().accept(MediaType.APPLICATION_XML)
				.get(User.class);
		System.out.println(user.getUserName());
		
		User user2 = target.request().accept(MediaType.APPLICATION_JSON)
				.get(User.class);
		System.out.println(user2.getUserId());
		
//		String jsonRes = target.request().accept(MediaType.TEXT_PLAIN)
//				.get(String.class);
//		System.out.println(jsonRes);

		// String responseMsg =
		// target.path("myresource").request().get(String.class);
	}

	// @Test
	// public void updateUser(){
	// r = Client.create().resource("http://localhost:8081/jersey/api/users");
	// User user = new User("002","ZhaoXiaoXuan",24);
	// ClientResponse response =
	// r.path(user.getUserId()).accept(MediaType.APPLICATION_XML)
	// .put(ClientResponse.class, user);
	// System.out.println(response.getStatus());
	// }
	// @Test
	// public void deleteUser(){
	// r = Client.create().resource("http://localhost:8081/jersey/api/users");
	// GenericType<JAXBElement<User>> generic = new
	// GenericType<JAXBElement<User>>() {};
	// JAXBElement<User> jaxbContact = r
	// .path("002")
	// .type(MediaType.APPLICATION_XML)
	// .get(generic);
	// User user = jaxbContact.getValue();
	// System.out.println(user.getUserId() + ": " + user.getUserName());
	// ClientResponse response = r.path("002").delete(ClientResponse.class);
	// System.out.println(response.getStatus());
	// }
}